#include <fstream>
#include <sstream>
#include "cResourceManager.h"
#include "Utils.h"
#include "../Utilities/utilities.h"
#include "Defines.h"

cResourceManager* cResourceManager::s_Instance = 0;
cResourceManager* cResourceManager::GetInstance()
{
	if(!s_Instance)
	{
		s_Instance =  new cResourceManager();
	}
	return s_Instance;
}

void cResourceManager::DestroyInstance()
{
	SAFE_DEL(s_Instance);
}

void cResourceManager::ReadResourceFile()
{
	char s[255];
	char url[255]; 
	char url_VS_Shader[255];
	char url_FS_Shader[255];
	FILE *pFile;
	int ipos;
	int id;
	fopen_s(&pFile,"Resource.txt","r");
	if(pFile == NULL)
	{
		printf("Can't read file\n");
	}
	char *buff = new char[MAX_LENGTH];

	//Model
	ReadLine(pFile,buff); 
	ScanNumberInt32(buff,&ipos,0);
	for( int i = 0 ;  i < ipos ; i++ )
	{
		ReadLine(pFile,buff);//id
		ScanNumberInt32(buff, &id,0);
		
		ReadLine(pFile,buff);//path
		memset(s,'\0', sizeof(s));
		memcpy(s, buff+5, strlen(buff+5));	
		list_UrlModel.push_back(s);
	}
	// texture
	ReadLine(pFile,buff); 
	ScanNumberInt32(buff,&ipos,0);
	for( int i = 0 ;  i < ipos ; i++ )
	{
		ReadLine(pFile,buff);//id
		ScanNumberInt32(buff, &id,0);
		
		ReadLine(pFile,buff);//path
		memset(url,'\0', sizeof(s));
		memcpy(url, buff+5, strlen(buff+5));
			
		list_UrlTexture.push_back(url);
	}
	// shader
	ReadLine(pFile,buff); 
	ScanNumberInt32(buff,&ipos,0);
	for( int i = 0 ;  i < ipos ; i++ )
	{
		ReadLine(pFile,buff);//id
		ScanNumberInt32(buff, &id,0);
		
		ReadLine(pFile,buff);//path
		memset(url_VS_Shader,'\0', sizeof(s));
		memcpy(url_VS_Shader, buff+3, strlen(buff+3));
		list_UrlShader.push_back(url_VS_Shader);
		ReadLine(pFile,buff);//path
		memset(url_FS_Shader,'\0', sizeof(s));
		memcpy(url_FS_Shader, buff+3, strlen(buff+3));
		list_UrlShader.push_back(url_FS_Shader);
	}
	
	SAFE_DEL(buff);
	fclose(pFile);
}

char* cResourceManager::getUrl_Model(int objId)
{
	string str = list_UrlModel[objId];

	char* st = new char[255];
	for ( int i = 0; i < (int)str.length() ; i++)
	{
		st[i] = str[i];
	}
	st[str.length()] = '\0';
	return st;
}

char* cResourceManager::getUrl_vShader(int objId)
{
	string str = list_UrlShader[objId * 2];

	char* st = new char[255];
	for ( int i = 0; i < (int)str.length() ; i++)
	{
		st[i] = str[i];
	}
	st[str.length()] = '\0';
	return st;
}

char* cResourceManager::getUrl_fShader(int objId)
{
	string str = list_UrlShader[(objId*2)+1];

	char* st = new char[255];
	for ( int i = 0; i < (int)str.length() ; i++)
	{
		st[i] = str[i];
	}
	st[str.length()] = '\0';
	return st;
}

char* cResourceManager::getUrl_Texture(int objId)
{
	string str = list_UrlTexture[objId];

	char* st = new char[255];
	for ( int i = 0; i < (int)str.length() ; i++)
	{
		st[i] = str[i];
	}
	st[str.length()] = '\0';
	return st;
}